<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="项目">
          <el-select
            v-model="searchParam.projectId"
            placeholder="请选择项目"
            clearable
          >
            <el-option
              v-for="item in projectList"
              :key="item.id"
              :label="item.projectName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工序">
          <el-select
            v-model="searchParam.processId"
            placeholder="请选择工序"
            clearable
          >
            <el-option
              v-for="item in processList"
              :key="item.id"
              :label="item.processName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="操作类型">
          <el-select
            v-model="searchParam.opeTypeCode"
            placeholder="请选择操作类型"
            clearable
            @change="changeOpeType"
          >
            <el-option
              v-for="item in useDictionary('CheckOperateType')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="事务类型">
          <el-select
            v-model="searchParam.effectTypeCode"
            placeholder="请选择事务类型"
            clearable
          >
            <el-option
              v-for="item in effectTypeCodeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="预检单号">
          <el-input
            v-model="searchParam.effectPreCheckCode"
            placeholder="请输入预检单号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetHandle"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button
          v-if="props.sopType === ''"
          type="primary"
          @click="openDialog1()"
          >新增预检单</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          label="预检单号"
          prop="effectPreCheckCode"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="事务类型"
          prop="effectTypeCode"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ getEffectTypeCode(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column
          label="检验项目名称"
          prop="checkItemName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="项目"
          prop="projectName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="工序"
          prop="processName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="检验规则名称"
          prop="ruleName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="检验类型"
          prop="checkTypeName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="检验状态"
          prop="effectCheckStatus"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{
              useDictionary("effectCheckStatus", scope.row.effectCheckStatus)
            }}
          </template>
        </el-table-column>
        <el-table-column
          label="预检数量"
          prop="preNum"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="已验数量"
          prop="completeNum"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="操作类型"
          prop="opeTypeCode"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("CheckOperateType", scope.row.opeTypeCode) }}
          </template>
        </el-table-column>
        <el-table-column
          label="检验级别"
          prop="effectLevelCode"
          min-width="100"
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("EffectLevel", scope.row.effectLevelCode) }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="330" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail1(scope.row)"
              v-if="props.sopType === ''"
              ><i-ep-search />
              查看预检单
            </el-button>
            <!-- <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
            >
              查看预检单
            </el-button> -->
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogItemRuledetail(scope.row)"
            >
              查看明细
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="dumpDoc(scope.row)"
            >
              查看检验单
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <qmscheckpre-detail
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      @change-visible="changeVisible"
    />
    <qmscheckpre-item-rule-detail
      :visible="itemRuleDialog.visible"
      :title="itemRuleDialog.title"
      :data="itemRuleDialog.data"
      :ruleData="itemRuleDialog.ruleData"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      @change-visible="changeItemRuleVisible"
    />

    <qmscheckpre-add
      :visible="addDialog1.visible"
      :title="addDialog1.title"
      :data="addDialog1.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog1.readvisible"
      @change-visible="changeVisible"
    />
    <!-- <qmscheckpre-mock
      :visible="addDialog1.visible"
      :title="addDialog1.title"
      :data="addDialog1.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog1.readvisible"
      @change-visible="changeVisible"
    /> -->
  </div>
</template>

<script setup lang="ts">
import { useDictionary, useTable } from "@/utils/useTable";
import request from "@/utils/request";
import { useRouter } from "vue-router";
import { checkCommon } from "@/views/qms/checkHook/checkCommon";

defineOptions({
  name: "QMSQMSCHECKPRE",
  inheritAttrs: false,
});

const props = defineProps({
  sopType: {
    type: String,
    default: "",
  },
});
const router = useRouter();
// 请求参数
const requestApi = "/qms/qmscheckpre/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  effectLevelCode: "",
  projectName: "",
  sopTypeCode: props.sopType,
};

const deleteParam = {
  api: "/qms/qmscheckpre/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);
const { effectTypeCodeList, getEffectTypeCode, changeOpeType, resetHandle } =
  checkCommon(searchParam, reset);
onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  title: "",
  data: {},
});

const itemRuleDialog = reactive<DialogOption>({
  visible: false,
  title: "",
  data: {},
  ruleData: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/qms/qmscheckpre/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

const getRule = async (checkRuleId: string) => {
  const { data } = await request({
    url: `/qms/qmscheckrule/get/${checkRuleId}`,
    method: "get",
  });
  itemRuleDialog.ruleData = data;
};

// 查询详情
async function getItemInfo(standardId: string) {
  await request({
    url: `/qms/qmssopstandard/get/${standardId}`,
    method: "get",
  })
    .then((data) => {
      itemRuleDialog.data = data.data;
      itemRuleDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开标准检验预检单表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改标准检验预检单";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增标准检验预检单";
    addDialog.visible = true;
  }
}

/** 打开标准检验预检单表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "标准检验预检单详情";
  getInfo(roleObj.id);
}

/** 打开标准检验预检单表单弹窗 详情 */
async function openDialogItemRuledetail(roleObj?: any) {
  itemRuleDialog.title = "预检单明细查看";
  await getRule(roleObj.checkRuleId);
  await getItemInfo(roleObj.standardId);
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  addDialog1.visible = value;
  if (!value) {
    addDialog.data = {};
    addDialog1.data = {};
  }
}

function changeItemRuleVisible(value: boolean) {
  itemRuleDialog.visible = value;
  if (!value) {
    itemRuleDialog.data = {};
  }
}

const projectList = ref([]);

const getProjectList = async () => {
  const { data } = await request({
    url: "/mdm/mdmprojectinfo/getList",
    method: "post",
    data: {},
  });
  projectList.value = data;
};

const processList = ref([]);

const getProcessList = async () => {
  const { data } = await request({
    url: "/mdm/mdmprocess/getList",
    method: "post",
    data: {},
  });
  processList.value = data;
};

onMounted(() => {
  getProjectList();
  searchParam.value.sopTypeCode = props.sopType;
  getProcessList();
});
const dumpDoc = (val) => {
  const path = props.sopType.slice(2);
  router.push({
    path: `/qms/${path === "" ? "qms" : path}CheckDoc`,
    query: {
      effectPreCheckCode: val.effectPreCheckCode,
    },
  });
};

const addDialog1 = reactive<DialogOption>({
  visible: false,
  title: "",
  readvisible: false,
  data: {},
});
/** 打开标准检验预检单表单弹窗 */
function openDialog1(roleObj?: Object) {
  if (roleObj) {
    addDialog1.title = "修改标准预检单";
    addDialog1.readvisible = false;
    getInfo1(roleObj.id);
  } else {
    addDialog1.title = "新增标准预检单";
    addDialog1.visible = true;
    addDialog1.readvisible = false;
  }
}

/** 打开标准检验预检单表单弹窗 详情 */
function openDialogdetail1(roleObj?: any) {
  addDialog1.title = "标准预检单详情";
  addDialog1.readvisible = true;
  getInfo1(roleObj.id);
}

// 查询详情
async function getInfo1(id: string) {
  request({
    url: `/qms/qmscheckpre/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog1.data = data.data;
      addDialog1.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}
</script>
